const { ClockInUrl, HabitUrl } = require("../../../config/config");

// pages/habit/statistics/statistics.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notLogin:false,
    clockinRate:28,
    currentYear:2020,
    currentMonth:7,
    currentHabit:[],
    clockins:[],
    hid:'',
    date:''
   
  },
  next: function (event) {
    this.changeMonth(event)
  },
  prev: function (event) {
    this.changeMonth(event)
  },
  changeMonth:function(event){
    let year = event.detail.currentYear
    let month = event.detail.currentMonth
    if(month < 10){
      month = '0'+month
    }
    let dateStr = year +'-'+ month;
    this.setData({
        date : dateStr,  
        currentMonth:event.detail.currentMonth,
        currentYear:event.detail.currentYear  
    })
    this.listCanlendar()
  },
  onLoad: function (options) {
    let _this = this
    let myDate = new Date();
    let year = myDate.getFullYear();
    let month = myDate.getMonth()+1;
    if(month < 10){
      month = '0'+month
    }
    let dateStr = year +'-'+ month;
    this.setData({
        hid : options.hid,
        date : dateStr ,
        currentYear:myDate.getFullYear(),
        currentMonth : myDate.getMonth()+1      
    })
    this.listCanlendar()
    this.onShow()

  },
  listCanlendar:function(){
    let _this = this
    wx.request({
      url: ClockInUrl +'/list_month',
      data: {
        hid : _this.data.hid,
        date: _this.data.date
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      },
      method: 'POST', 
      success: function(res){
        let dateItems = res.data.result
        let objs = []
        for(let i=0; i<dateItems.length; i++){
          let obj = {
            month:"current",
            day:"",
            color:"#ffffff",
            background:"#6887E2"
          }
          obj.day = dateItems[i]
          objs.push(obj)
        }
        let rate = parseInt(objs.length/_this.mGetDate(_this.data.currentYear,_this.data.currentMonth)*100)
        _this.setData({
          clockins : objs,
          clockinRate :　rate
        })
      }
    }) 
  },
  onShow: function () {
    let _this = this
    wx.request({
      url: HabitUrl+'/'+_this.data.hid,
      method: 'GET', 
      success: function(res){
        _this.setData({
          currentHabit : res.data.result
        })
      }
    })
  },
  mGetDate :function(year, month){
    var d = new Date(year, month, 0);
    return d.getDate();
  }
})